package cn.sumpu.toolkit.email;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import cn.sumpu.toolkit.common.util.Common;

public class EmailProxy {

	private String mailUsername = "hi@sumpu.cn";
	private String mailPassword = "tommy";
	private String mailEmail = "hi@sumpu.cn";

	private Properties props;
	private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(
			100);
	private ThreadPoolExecutor exec = new ThreadPoolExecutor(5, 10, 60,
			TimeUnit.SECONDS, queue);

	public void sendEmail(final String title, final String emailContent) {
		Future f = exec.submit(new Send(title ,emailContent));
		try {
			f.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}

	class Send implements Runnable {

		private String title;
		private String emailContent;

		Send(String title, String emailContent) {
			this.title = title;
			this.emailContent = emailContent;
		}

		@Override
		public void run() {
			Properties p = new Properties();
			p.put("mail.smtp.host", "mail.sumpu.cn");
			p.put("mail.smtp.auth", "true");
			Session session = Session.getInstance(/*props*/p,
					new SimpleAuthenticator());
			session.setDebug(true);
			MimeMessage msg = new MimeMessage(session);
			try {
				msg.setFrom(new InternetAddress(mailEmail, "果果街")); 
				String toEmail = "Yu.Zhang0713@gmail.com";
				InternetAddress[] address = { new InternetAddress(toEmail) };
				msg.setRecipients(Message.RecipientType.TO, address);
				msg.setSentDate(new Date());
				if(Common.notEmptyStr(title)){
					msg.setSubject(title);
				}else{
					msg.setSubject("欢迎入住果果街-注册邮箱验证信");
				}
				//构造登录路径
				if(Common.notEmptyStr(emailContent)){
					msg.setContent(emailContent,"text/html ;charset=utf-8");
				}else{
					msg.setContent("您好：<a>test</a>", 
							"text/html ;charset=utf-8");
				}
				Transport.send(msg);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (MessagingException e) {
				e.printStackTrace();
			}
		}
	}

	public String getMailUsername() {
		return mailUsername;
	}

	public void setMailUsername(String mailUsername) {
		this.mailUsername = mailUsername;
	}

	public String getMailPassword() {
		return mailPassword;
	}

	public void setMailPassword(String mailPassword) {
		this.mailPassword = mailPassword;
	}

	public String getMailEmail() {
		return mailEmail;
	}

	public void setMailEmail(String mailEmail) {
		this.mailEmail = mailEmail;
	}

	public Properties getProps() {
		return props;
	}

	public void setProps(Properties props) {
		this.props = props;
	}

	class SimpleAuthenticator extends Authenticator {

		@Override
		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(/*mailUsername*/"hi@sumpu.cn", /*mailPassword*/"tommy");
		}

	}

}
